//
//  DistrictExtention.m
//  VietQuan
//
//  Created by hainguyen on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DistrictExtention.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"

@implementation District(Extention)

#pragma mark Init Data Methods

+ (BOOL) initDistrict:(int)provinceId districtList:(NSArray*)districtList{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil)
	{
		return NO;
	}
	if (districtList != nil) {
		for (int i = 0; i < [districtList count]; i++) {
			District *district = [NSEntityDescription insertNewObjectForEntityForName:@"District" inManagedObjectContext:managedObjectContext];
			district.id = [NSNumber numberWithInt:i];
			district.provinceId = [NSNumber numberWithInt:provinceId];
			district.name = [districtList objectAtIndex:i];
		}
		[managedObjectContext save:&err];
		return YES;
	}
		
	return NO;
}

#pragma mark Get Methods

+ (NSArray *) getDistricts{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"District" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;
}

+ (District *) getDistrict:(int)districtId{
	District *district = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *districtArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"DistrictId = %@", districtId];	
	@try {
		districtArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"District" :predicate :nil :NO :managedObjectContext];
		
		if ([districtArray count] != 0)
		{
			district = [districtArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return district;
}
+ (NSArray *) getDistrictWithProvince:(int)provinceId{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *postArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"provinceId = %d", provinceId];	
	@try {
		postArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"District" :predicate :nil :NO :managedObjectContext];
		
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return postArray;
}

#pragma mark Search Methods

+ (NSArray*)searchByName:(NSString*) keyword{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"District" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by name error: %@", [e description]);
    }
    
    return resultArray;
}


@end
